﻿using System;
using System.Collections.Generic;
using Sustainalytics.ClientDW.Entities;
using Xunit;
using System.Linq;
using Sustainalytics.Entities;

namespace Sustainalytics.Portfolios.BL.Tests.UnitTest
{
    [Trait("Category", "Unit")]
    public class MatchingAlgorithmUnitTest
    {
        private readonly MatchingSecuritiesAlgorithm _sut;
        public MatchingAlgorithmUnitTest()
        {
            _sut = new MatchingSecuritiesAlgorithm();
        }

        [Fact]
        public void Matches_Two_Different_securities_same_company_select_Main()
        {

            IList<Company> companySearches = new[]
            {
                new Company
                {
                    CompanyId = 1057062927,
                    CompanyName = "test",
                    MainTicker="SEA",
                    Country="Austria",
                    SubPeerGroupId=25203612,
                },
                new Company()
                {
                    CompanyId = 1057062927,
                    CompanyName = "test",
                    MainTicker="SEA",
                    Country="Austria",
                    SubPeerGroupId=25203612,
                }
            };


            var result = _sut.GetSecurity(companySearches);

            Assert.True(result.Matches.First()?.Country == "Austria");
        }


        [Fact]
        public void Matches_Two_Different_securities_same_company_None_marked_as_Main()
        {

            IList<Company> companySearches = new[]
            {
                new Company
                {
                    CompanyId = 1057062927,
                    CompanyName = "test",
                    MainTicker="SEA",
                    Country="Austria",
                    SubPeerGroupId=25203612,
                },
                new Company()
                {
                    CompanyId = 1057062927,
                    CompanyName = "test",
                    MainTicker="SEA",
                    Country="Austria",
                    SubPeerGroupId=25203612,
                }
            };


            var result = _sut.GetSecurity(companySearches);
            Assert.True(result.MatchType == PortfolioMatchType.SecuritiesMatchSameCompany);
        }


        [Fact]
        public void Matches_Two_Different_securities_same_company_more_than_one_marked_as_Main()
        {

            IList<Company> companySearches = new[]
            {
                new Company
                {
                    CompanyId = 1057062927,
                    CompanyName = "test",
                    MainTicker="SEA",
                    Country="Austria",
                    SubPeerGroupId=25203612,
                },
                new Company()
                {
                    CompanyId = 1057062927,
                    CompanyName = "test",
                    MainTicker="DOO",
                    Country="Austria",
                    SubPeerGroupId=25203642,
                }
            };



            var result = _sut.GetSecurity(companySearches);

            Assert.True(result.Matches != null);
        }




        [Fact]
        public void ExactMatch_should_return_one_identifier()
        {

            IList<Company> companySearches = new[]
       {
                new Company
                {
                    CompanyId = 1057062927,
                    CompanyName = "test",
                    MainTicker="SEA",
                    Country="Austria",
                    SubPeerGroupId=25203612,

                }
            };


            var result = _sut.GetSecurity(companySearches);

            Assert.True(result.Matches.First()?.CompanyId == 1057062927 && result.Matches.First()?.MainTicker== "SEA");
        }

    }

}
